﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Web.UI;
using System.Web.UI.WebControls;
using BLL.Common;
using Module;
using BLL;

namespace App
{
    public partial class BBS_EditUserPost : System.Web.UI.Page
    {
        protected void Page_Load(object sender, EventArgs e)
        {            
            if (!Page.IsPostBack)
            {
                pageInit();
            }
        }

        protected void btn_OnClick(object sender, EventArgs e)
        {
            if (!AppAuthHelper.SessionCheck() || AppAuthHelper.CurrentUserPassportId == 0 || ViewState["UserPostInfo"]==null)
            {
                Community.Utility.JavaScriptHelper.LocationPageAfterMessageBox(this, "请重新登录后，再修改评论", Contexts.ThisPageUrlWithParams);
            }
            else
            {
                UserPostInfo post = ViewState["UserPostInfo"] as UserPostInfo;
                if (post == null || post.UserPostId == 0)
                {
                    Community.Utility.JavaScriptHelper.LocationPageAfterMessageBox(this, "请重新登录后，再修改评论", Contexts.ThisPageUrlWithParams);
                    return;
                }
                post.Body = this.content.Value;
                post.LastUpdate = DateTime.Now;

                BUserPost.getInstance().Update(post);

                Community.Utility.JavaScriptHelper.LocationPageAfterMessageBox(this, "修改成功", Contexts.AppPath + "/BBS/ThreadInfo.aspx?"+Contexts.ThreadId+"="+Contexts.GetEncrypt(post.UserPaperId.ToString()));
            }
        }

        private void pageInit()
        {
            //if (AppAuthHelper.CurrentUserPassportId == 0)
            //{
            //    Response.Redirect("ThreadList.aspx");
            //}
            long userPostId = Contexts.GetAPPUserPostId("/BBS/ThreadList.aspx");
            long paperId = Contexts.GetAPPThreadId("/BBS/ThreadList.aspx");

            if (userPostId > 0)
            {
                UserPostInfo post = BUserPost.getInstance().GetPostByUserPostId(userPostId);
                if (post != null && post.UserPaperId == paperId)
                {
                    this.content.Value = post.Body;
                    ViewState["UserPostInfo"] = post;
                    this.hf_UserPostId.Value = post.UserPostId.ToString();
                }
            }

        }
    }
}